﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Xml
{
    /// <summary>
    /// 输出基类表
    /// </summary>
    public class CreateTable
    {
        public string GetTableString(string tableName)
        {
            System.Collections.Generic.List<T_Field> fields = XMLReader.GetFields(tableName);
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("CREATE TABLE [" + tableName + "](");

            for (int i = 0; i < fields.Count; i++)
            {
                if (fields[i].Field.ToLower().Equals("id"))
                {
                    sb.AppendLine("	[Id] [int] IDENTITY(1,1) NOT NULL,");
                }
                else
                {
                    if (fields[i].Attribute.ToLower().Equals("int?"))
                    {
                        sb.AppendLine("	[" + fields[i].Field + "] [int],");
                    }
                    else if (fields[i].Attribute.ToLower().Equals("string"))
                    {
                        sb.AppendLine("	[" + fields[i].Field + "] [nvarchar](max),");
                    }
                    else if (fields[i].Attribute.ToLower().Equals("datetime"))
                    {
                        sb.AppendLine("	[" + fields[i].Field + "] [DateTime],");
                    }
                    else if (fields[i].Attribute.ToLower().Equals("decimal?"))
                    {
                        sb.AppendLine("	[" + fields[i].Field + "] [float],");
                    }
                    else if (fields[i].Attribute.ToLower().Equals("bool?"))
                    {
                        sb.AppendLine("	[" + fields[i].Field + "] [bit],");
                    }
                }
            }
            sb.AppendLine(" CONSTRAINT [PK_" + tableName + "] PRIMARY KEY CLUSTERED (");
            sb.AppendLine("[Id] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY] ");
            return sb.ToString();
        }
    }
}
